package com.innovate.nms.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.google.common.eventbus.Subscribe;
import com.innovate.nms.common.Ping;
import com.innovate.nms.domain.AppConfig;
import com.innovate.nms.domain.NetNode;
import com.innovate.nms.service.inspect.InspectNetTask;
import com.innovate.nms.service.inspect.InspectTask;
import com.ivan.toolkit.common.Events;

public class InspectGraphicPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6729991985065365998L;
	private JLabel labelEMS;
	private JLabel labelEMU1;
	private JLabel labelEMU2;
	private JLabel labelEMU3;
	private JLabel labelRTU;
	private JLabel lblEmu1IP;
	private JLabel lblEmu2IP;
	private JLabel lblEmu3IP;
	private JLabel lblRtuIP;
	private JLabel lblEmu1SMS;
	private JLabel lblEmu2SMS;
	private JLabel lblEmu3SMS;
	private SplashPanel paneEMS;
	private SplashPanel paneEMU1;
	private SplashPanel paneEMU2;
	private SplashPanel paneEMU3;
	private SplashPanel paneRTU;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JLabel label_4;
	
	private NetNode selected = null;
	private long selectedTime = 0L;

	/**
	 * Create the panel.
	 */
	public InspectGraphicPanel() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 1200, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 360, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 1;
		add(panel, gbc_panel);
		
		paneEMS = new SplashPanel();
		paneEMS.setImage(Toolkit.getDefaultToolkit().getImage(InspectGraphicPanel.class.getResource("/images/local.png")));
		paneEMS.setBounds(105, 50, 108, 123);
		panel.add(paneEMS);
		
		paneEMU1 = new SplashPanel();
		paneEMU1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2) {
					AppConfig config = AppConfig.instance.get();
					NetNode node = config.getEmu1();
					if(e.isShiftDown()) {
						selectNode(node);
					} else if(e.isControlDown()) {
						Ping.systemPing(node.getIp());
					} else {	
						checkNode(node);
					}
					
				}
			}
		});
		paneEMU1.setImage(Toolkit.getDefaultToolkit().getImage(InspectGraphicPanel.class.getResource("/images/emu.png")));
		paneEMU1.setBounds(318, 50, 108, 123);
		panel.add(paneEMU1);
		
		paneEMU2 = new SplashPanel();
		paneEMU2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2) {
					AppConfig config = AppConfig.instance.get();
					NetNode node = config.getEmu2();
					if(e.isShiftDown()) {
						selectNode(node);
					} else if(e.isControlDown()) {
						Ping.systemPing(node.getIp());
					} else {	
						checkNode(node);
					}
					
				}
			}
		});
		paneEMU2.setImage(Toolkit.getDefaultToolkit().getImage(InspectGraphicPanel.class.getResource("/images/emu.png")));
		paneEMU2.setBounds(531, 50, 108, 123);
		panel.add(paneEMU2);
		
		paneEMU3 = new SplashPanel();
		paneEMU3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2) {
					AppConfig config = AppConfig.instance.get();
					NetNode node = config.getEmu3();
					if(e.isShiftDown()) {
						selectNode(node);
					} else if(e.isControlDown()) {
						Ping.systemPing(node.getIp());
					} else {	
						checkNode(node);
					}
					
				}
			}
		});
		paneEMU3.setImage(Toolkit.getDefaultToolkit().getImage(InspectGraphicPanel.class.getResource("/images/emu.png")));
		paneEMU3.setBounds(744, 50, 108, 123);
		panel.add(paneEMU3);
		
		paneRTU = new SplashPanel();
		paneRTU.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2) {
					AppConfig config = AppConfig.instance.get();
					NetNode node = config.getRtu();
					if(e.isShiftDown()) {
						selectNode(node);
					} else if(e.isControlDown()) {
						Ping.systemPing(node.getIp());
					} else {	
						checkNode(node);
					}
					
				}
			}
		});
		paneRTU.setImage(Toolkit.getDefaultToolkit().getImage(InspectGraphicPanel.class.getResource("/images/rtu.png")));
		paneRTU.setBounds(957, 50, 108, 123);
		panel.add(paneRTU);
		
		JPanel panel_line = new JPanel();
		panel_line.setBackground(Color.GRAY);
		panel_line.setBounds(105, 112, 960, 4);
		panel.add(panel_line);
		
		labelEMS = new JLabel("EMS");
		labelEMS.setHorizontalAlignment(SwingConstants.CENTER);
		labelEMS.setForeground(new Color(70, 130, 180));
		labelEMS.setFont(new Font("微软雅黑", Font.BOLD, 16));
		labelEMS.setBounds(105, 233, 108, 26);
		panel.add(labelEMS);
		
		labelEMU1 = new JLabel("EMU1");
		labelEMU1.setHorizontalAlignment(SwingConstants.CENTER);
		labelEMU1.setForeground(new Color(70, 130, 180));
		labelEMU1.setFont(new Font("微软雅黑", Font.BOLD, 16));
		labelEMU1.setBounds(317, 233, 108, 27);
		panel.add(labelEMU1);
		
		labelEMU2 = new JLabel("EMU2");
		labelEMU2.setHorizontalAlignment(SwingConstants.CENTER);
		labelEMU2.setForeground(new Color(70, 130, 180));
		labelEMU2.setFont(new Font("微软雅黑", Font.BOLD, 16));
		labelEMU2.setBounds(530, 233, 108, 27);
		panel.add(labelEMU2);
		
		labelEMU3 = new JLabel("EMU3");
		labelEMU3.setHorizontalAlignment(SwingConstants.CENTER);
		labelEMU3.setForeground(new Color(70, 130, 180));
		labelEMU3.setFont(new Font("微软雅黑", Font.BOLD, 16));
		labelEMU3.setBounds(744, 233, 108, 27);
		panel.add(labelEMU3);
		
		labelRTU = new JLabel("RTU");
		labelRTU.setHorizontalAlignment(SwingConstants.CENTER);
		labelRTU.setForeground(new Color(70, 130, 180));
		labelRTU.setFont(new Font("微软雅黑", Font.BOLD, 16));
		labelRTU.setBounds(956, 233, 108, 27);
		panel.add(labelRTU);
		
		lblEmu2IP = new JLabel("255.255.255.255");
		lblEmu2IP.setHorizontalAlignment(SwingConstants.CENTER);
		lblEmu2IP.setForeground(new Color(70, 130, 180));
		lblEmu2IP.setFont(new Font("微软雅黑", Font.BOLD, 16));
		lblEmu2IP.setBounds(507, 261, 155, 26);
		panel.add(lblEmu2IP);
		
		lblEmu1IP = new JLabel("255.255.255.255");
		lblEmu1IP.setHorizontalAlignment(SwingConstants.CENTER);
		lblEmu1IP.setForeground(new Color(70, 130, 180));
		lblEmu1IP.setFont(new Font("微软雅黑", Font.BOLD, 16));
		lblEmu1IP.setBounds(294, 261, 155, 26);
		panel.add(lblEmu1IP);
		
		lblEmu1SMS = new JLabel("255.255.255.255");
		lblEmu1SMS.setHorizontalAlignment(SwingConstants.CENTER);
		lblEmu1SMS.setForeground(new Color(70, 130, 180));
		lblEmu1SMS.setFont(new Font("微软雅黑", Font.BOLD, 16));
		lblEmu1SMS.setBounds(294, 292, 155, 26);
		panel.add(lblEmu1SMS);
		
		lblEmu2SMS = new JLabel("255.255.255.255");
		lblEmu2SMS.setHorizontalAlignment(SwingConstants.CENTER);
		lblEmu2SMS.setForeground(new Color(70, 130, 180));
		lblEmu2SMS.setFont(new Font("微软雅黑", Font.BOLD, 16));
		lblEmu2SMS.setBounds(507, 292, 155, 26);
		panel.add(lblEmu2SMS);
		
		lblEmu3SMS = new JLabel("255.255.255.255");
		lblEmu3SMS.setHorizontalAlignment(SwingConstants.CENTER);
		lblEmu3SMS.setForeground(new Color(70, 130, 180));
		lblEmu3SMS.setFont(new Font("微软雅黑", Font.BOLD, 16));
		lblEmu3SMS.setBounds(720, 292, 155, 26);
		panel.add(lblEmu3SMS);
		
		lblEmu3IP = new JLabel("255.255.255.255");
		lblEmu3IP.setHorizontalAlignment(SwingConstants.CENTER);
		lblEmu3IP.setForeground(new Color(70, 130, 180));
		lblEmu3IP.setFont(new Font("微软雅黑", Font.BOLD, 16));
		lblEmu3IP.setBounds(720, 261, 155, 26);
		panel.add(lblEmu3IP);
		
		lblRtuIP = new JLabel("255.255.255.255");
		lblRtuIP.setHorizontalAlignment(SwingConstants.CENTER);
		lblRtuIP.setForeground(new Color(70, 130, 180));
		lblRtuIP.setFont(new Font("微软雅黑", Font.BOLD, 16));
		lblRtuIP.setBounds(933, 261, 155, 26);
		panel.add(lblRtuIP);
		
		label = new JLabel("EMS");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(new Color(70, 130, 180));
		label.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label.setBounds(105, 197, 108, 26);
		panel.add(label);
		
		label_1 = new JLabel("EMU1");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setForeground(new Color(70, 130, 180));
		label_1.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label_1.setBounds(318, 196, 108, 27);
		panel.add(label_1);
		
		label_2 = new JLabel("EMU2");
		label_2.setHorizontalAlignment(SwingConstants.CENTER);
		label_2.setForeground(new Color(70, 130, 180));
		label_2.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label_2.setBounds(531, 196, 108, 27);
		panel.add(label_2);
		
		label_3 = new JLabel("EMU3");
		label_3.setHorizontalAlignment(SwingConstants.CENTER);
		label_3.setForeground(new Color(70, 130, 180));
		label_3.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label_3.setBounds(744, 196, 108, 27);
		panel.add(label_3);
		
		label_4 = new JLabel("RTU");
		label_4.setHorizontalAlignment(SwingConstants.CENTER);
		label_4.setForeground(new Color(70, 130, 180));
		label_4.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label_4.setBounds(957, 196, 108, 27);
		panel.add(label_4);

		update();
		
		Events.register(this);
	}
	
	@Subscribe
	public void onEvent(Events event) {
		
		if(event.getKey() instanceof AppConfig) {
			update();
		}
		
		if(event.keyEqualWith(InspectTask.EventKey)) {
			
			InspectTask source = event.get("source", InspectTask.class);
			if(source == null) return;
			
			if(source.getState() == InspectTask.State.pending) {
				
				String cls = event.get("class", String.class);
				update(cls);
				
			} else {
				
				update("");
			}
			
		}
		
	}
	
	private void update(String event) {
		
		if(event == null) event = "";
		
		event = event.toLowerCase();
		this.paneEMS.setSplash(event.contains("ems"));
		this.paneEMU1.setSplash(event.contains("emu1"));
		this.paneEMU2.setSplash(event.contains("emu2"));
		this.paneEMU3.setSplash(event.contains("emu3"));
		this.paneRTU.setSplash(event.contains("rtu"));
	}
	
	private void update() {
		
		AppConfig config = AppConfig.instance.get();
		
		this.labelEMS.setText(config.getLocalName());
		this.labelEMU1.setText(config.getEmu1().getName());
		this.labelEMU2.setText(config.getEmu2().getName());
		this.labelEMU3.setText(config.getEmu3().getName());
		this.labelRTU.setText(config.getRtu().getName());
		
		this.lblEmu1IP.setText(config.getEmu1().getIp());
		this.lblEmu1SMS.setText(config.getEmu1().getSms());
		
		this.lblEmu2IP.setText(config.getEmu2().getIp());
		this.lblEmu2SMS.setText(config.getEmu2().getSms());
		
		this.lblEmu3IP.setText(config.getEmu3().getIp());
		this.lblEmu3SMS.setText(config.getEmu3().getSms());
		
		this.lblRtuIP.setText(config.getRtu().getIp());
		
	}
	
	private void selectNode(NetNode node) {
		
		long now = System.currentTimeMillis();
		
		if(selected != null) {
			if((now - selectedTime) > 2000) {
				selected = node;
				selectedTime = now;
			} else {
				InspectNetTask task = new InspectNetTask(selected, node);
				task.execute();
			}
		} else {
			selected = node;
			selectedTime = now;
		}
		
	}
	
	private void checkNode(NetNode node) {
		InspectNetTask task = new InspectNetTask(node);
		task.execute();
	}
	
}
